import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import { Toast } from 'antd-mobile';
import Utils from '../../../utils/index'
import DB from '../../../app/db';
import nattyStorage from 'natty-storage';
import StorageUtils from '../../../app/storage'
import { Control } from 'react-keeper'

const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-wechat',
});

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    userInfo: {},
    doors: []
  },

  init() {
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    this.updateComponent();
  },

  initMode: function () {
    let t = this;
    t.data.userInfo = update(t.data.userInfo, {doors: {$set: []}});
    t.data.userInfo = update(t.data.userInfo, {courts: {$set: []}});
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
  },

  onEnterInfo: function (e) {
    let t = this;
    DB.WechatAPI.getInfoByOpenId({
      openId: this.data.userInfo.openId,
    }).then(
      function (content) {
        if (content.length > 0) {
          StorageUtils.setStorage('doors', content);
          t.data.doors = update(t.data.doors, {$set: StorageUtils.getStorageItem('doors')});
          t.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
    /*let t = this;
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
    if(this.data.userInfo.id !== 'xxx')
    {
      DB.WechatAPI.getInfoByOpenId({
        openId: this.data.userInfo.openId,
      }).then(
        function (content) {
          let resultObj = Utils.isEmptyObject(content);
          if (resultObj.success) {
            if (content.length > 0) {
              t.data.userInfo = update(t.data.userInfo, {doors: {$set: content}});
              t.updateComponent();
            }
            t.data.showPhoneOK = update(t.data.showPhoneOK, {$set: true});
            t.data.showCertOK = update(t.data.showCertOK, {$set: true});
            t.updateComponent();
          }
        },
        function (error) {
          console.log(error);
        }
      );
    }*/

  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});

